package com.example.springbootdemo1.demos.web;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/UseController")
@RestController
public class UseController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    //请求单个参数
    @RequestMapping("/r1")
    public String r1(String name) {

        return "收到的参数name:" + name;

    }

    @RequestMapping("/r2")
    public String r2(Integer age) {

        return "收到的参数age:" + age;

    }

    //请求多个参数
    @RequestMapping("r3")
    public String r3(String name, Integer age) {
        return "name:" + name + ",age:" + age;

    }

    //传递对象
    @RequestMapping("r4")
    public String r4(User user) {
        return user.toString();
    }

    //重命名参数    以及设置参数非必传
    @RequestMapping("r5")
    public String r5(@RequestParam("name") String usename, @RequestParam(value = "age",required = false) Integer userage) {
        return "usename:" + usename + ",userage:" + userage;

    }
    //传递数组
    @RequestMapping("r6")
    public  String r6(String[]arr){
        return Arrays.toString(arr);
    }
    //传递集合
    @RequestMapping("r7")
    public  String r7(@RequestParam(value = "list",required = false) List<String>list){
        if(list!=null){
            return list.toString();
        }
       return "list is null";
    }
    //传递json
    @RequestMapping("/r8")
    public String r8(@RequestBody User user) {
        return user.toString();

    }
    //获取到URL中的参数
    @RequestMapping("/r9/{articleId}")
    public  String r9(@PathVariable Integer articleId){
        return "articleId" + articleId;
    }
    @RequestMapping("/r10/{name}/{age}")
    public  String r10(@PathVariable Integer age,@PathVariable("name") String usename){
        return  "username:"+usename+",age:" + age;
    }
    //上传文件
    @RequestMapping("r11")
    public String r11(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/新建文件夹/"+fileName));
        return "获取到上传文件："+file.getOriginalFilename();
    }

}